package com.yunxin.core.infra.persistence.repository.user;

import com.yunxin.core.domain.model.user.UserInfo;
import com.yunxin.core.domain.model.user.UserInfoRepository;
import com.yunxin.core.infra.persistence.converter.UserInfoConverter;
import com.yunxin.core.infra.persistence.entity.user.UserInfoDO;
import com.yunxin.core.infra.persistence.mapper.user.UserInfoExtendMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zero°
 * @since 2023-07-13
 */
@Repository
@RequiredArgsConstructor
public class UserInfoRepositoryImpl implements UserInfoRepository {

    private final UserInfoExtendMapper userInfoExtendMapper;

    private final UserInfoConverter userInfoConverter;

    @Override
    public boolean saveUserInfo(UserInfo userInfo) {
        UserInfoDO userInfoDO = userInfoConverter.formUserInfo(userInfo);
        userInfoDO.setIsAbutment(2);
        int count = userInfoExtendMapper.insertSelective(userInfoDO);
        return count > 0;
    }

    @Override
    public UserInfo getUserInfoByPhone(String phone) {
        UserInfoDO userInfoDO = userInfoExtendMapper.selectUserInfoByPhone(phone);
        return userInfoConverter.toUserInfo(userInfoDO);
    }

    @Override
    public UserInfo getUserInfoByMd5Phone(String md5Phone) {
        UserInfoDO userInfoDO = userInfoExtendMapper.selectUserInfoByMd5Phone(md5Phone);
        return userInfoConverter.toUserInfo(userInfoDO);
    }

    /**
     * 根据手机号前9位查询是否存在
     *
     * @param phone
     * @return
     */
    @Override
    public List<String> getMd5PhoneByPhoneNine(Integer phone) {
        return userInfoExtendMapper.getMd5PhoneByPhoneNine(phone);
    }

    /**
     * 测试，查询手机号
     * @return
     */
    public List<String> findAllAesPhone() {
        return userInfoExtendMapper.findAllAesPhone();
    }

    public void savePhoneNine(String aes, String phone) {
        userInfoExtendMapper.savePhoneNine(aes,phone);
    }

    public List<Xto> xtos() {
        return userInfoExtendMapper.xtos();
    }

    public void updateX(List<Xto> xtos) {
        userInfoExtendMapper.updateX(xtos);
    }
}
